package com.yizisu.library.ad.impl.ironsource

import androidx.lifecycle.MutableLiveData
import com.ironsource.mediationsdk.logger.IronSourceError
import com.ironsource.mediationsdk.model.Placement
import com.ironsource.mediationsdk.sdk.RewardedVideoListener
import com.yizisu.library.ad.IAdRewardStatus
import com.yizisu.library.ad.IAdStatus

object IronSourceVideoListener : RewardedVideoListener {
    var rewardListener: Function1<IAdRewardStatus, Unit>? = null
    val statusData = MutableLiveData<IAdStatus>()

    override fun onRewardedVideoAdOpened() {
        rewardListener?.invoke(IAdRewardStatus.OpenAd)
    }

    override fun onRewardedVideoAdClosed() {
        rewardListener?.invoke(IAdRewardStatus.Cancel)
        rewardListener = null
    }

    override fun onRewardedVideoAvailabilityChanged(available: Boolean) {
        if (available) {
            statusData.postValue(IAdStatus.Success)
        } else {
            statusData.postValue(IAdStatus.Error)
        }
    }

    override fun onRewardedVideoAdStarted() {
    }

    override fun onRewardedVideoAdEnded() {
    }

    override fun onRewardedVideoAdRewarded(p0: Placement?) {
        rewardListener?.invoke(IAdRewardStatus.Success)
        rewardListener = null
    }

    override fun onRewardedVideoAdShowFailed(p0: IronSourceError?) {
        statusData.postValue(IAdStatus.Error)
    }

    override fun onRewardedVideoAdClicked(p0: Placement?) {
    }
}